#include "../../inc/skyCanvas.h"
#ifdef _GLES_
#include "./glApp.h"
#include "../../inc/skyPict.h"
#include "../../inc/myResManager.h"
namespace Sky
{

    Bool Sky::Canvas::init( myResMgr* mgr,i32 w,i32 h,i32 Format_ ,u32 subFormat)
    {
		do 
		{
			_size = Size(w,h);
			_surf = GL::CreateRenderSurface(w,h,Format_);
			_pict = new Pict();
			_pict->setDesiredSize(Size(w,h));

			E_Break_If(!mgr->createPictFromBuffer(_pict,vNull));

			E_Break_If(!eglMakeCurrent(GL::getContext()->m_eglDisplay, _surf, _surf, GL::getContext()->m_eglContext));

			// Bind the texture to this surface
			// Bind the texture to this surface
			eglBindTexImage(GL::getContext()->m_eglDisplay, _surf, EGL_BACK_BUFFER);

			return vTrue;
		} while (0);
		return vFalse;
    }	

    Void Canvas::unInit()
    {
		SAFE_DELETE(_pict);
		GL::DestroyRenderSurface(_surf);
    }

    Bool Canvas::attachPict( Pict* pict )
    {
		xAssert(pict);
		if(_surf == vNull)
		{
			do 
			{
				_size = Size(pict->getSize().w,pict->getSize().h);

				_surf = GL::CreateRenderSurface(_size.w,_size.h,0);

				_pict = pict;

				glBindTexture(GL_TEXTURE_2D, pict->getTexture());

				E_Break_If(!eglMakeCurrent(GL::getContext()->m_eglDisplay, _surf, _surf, GL::getContext()->m_eglContext));

				// Bind the texture to this surface
				// Bind the texture to this surface
				eglBindTexImage(GL::getContext()->m_eglDisplay, _surf, EGL_BACK_BUFFER);


				return vTrue;
			} while (0);
		}
		_pict = pict;
		return vTrue;
    }

    Pict* Canvas::detachPict()
    {
		_pict = vNull;
        return vNull;
    }

}
#endif
